package com.qingzhu.dao.impl;

import com.qingzhu.dao.UserDao;
import com.qingzhu.pojo.User;
import com.qingzhu.util.CreateSqlSessionFactory;
import org.apache.ibatis.session.SqlSession;

public class UserDaoImpl implements UserDao {

    @Override
    public User findById(int id) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            return mapper.findById(id);
        }
    }

    @Override
    public int updateById(User user) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            return mapper.updateById(user);
        }
    }

    @Override
    public int insertUser(User user) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            return mapper.insertUser(user);
        }
    }

    @Override
    public User findUserFromPhone(String phone) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            return mapper.findUserFromPhone(phone);
        }
    }
}
